package cc.mrbird.febs.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * HTTP上下文工具类
 * 提供获取当前HTTP请求对象的功能
 * 基于Spring的RequestContextHolder实现
 * 
 * @author MrBird
 */
public class HttpContextUtil {

	/**
	 * 私有构造函数，防止实例化
	 */
	private HttpContextUtil() {
	}

	/**
	 * 获取当前HTTP请求对象
	 * 从Spring的RequestContextHolder中获取当前请求的ServletRequestAttributes
	 * 然后从中提取HttpServletRequest对象
	 *
	 * @return 当前请求的HttpServletRequest对象
	 * @throws NullPointerException 当没有活动的请求上下文时抛出
	 */
	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
	}
}
